package com.boot2523.myrabbitmq.q5;

import com.rabbitmq.client.*;

public class Stock {
    private static final String EXCHANGE_NAME = "topic_logs";
    private static final String BINDING_KEY = "stock.*";

    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "topic");

        String queueName = channel.queueDeclare().getQueue();

        channel.queueBind(queueName, EXCHANGE_NAME, BINDING_KEY);

        System.out.println(" [*] Stock waiting for messages with binding key: " + BINDING_KEY);

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            String routingKey = delivery.getEnvelope().getRoutingKey();
            System.out.println(" [STOCK] Received (" + routingKey + "): " + message);
        };

        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {});
    }
}
